import { SelectControlSchema } from 'amis/lib/renderers/Form/Select'
import { TableColumnObject } from 'amis/lib/renderers/Table'
import lodash from 'lodash'
import oauth2RegisteredClientService from '@/core/oauth2/service/oauth2RegisteredClientService'

class IdOfClientColumnUtils {
	getSchema = (parmas?: SelectControlSchema): TableColumnObject => {
		const name = parmas?.name ? parmas.name : 'idOfClient'
		const mergeSchema = Object.assign(
			{
				type: 'service',
				name: name,
				label: '客户端',
				dataProvider: (data: any, setData: Function) => {
					const id = data[name]
					oauth2RegisteredClientService.getByIdsWithDebounce(id).then(list => {
						let value = '-'
						lodash.forEach(list, item => {
							if (item.id === id) {
								value = item.clientName
							}
						})
						setData({ clientName: value })
					})
				},
				body: {
					type: 'tpl',
					tpl: '${clientName}',
				},
			},
			parmas,
		)

		const schema = mergeSchema
		return schema as TableColumnObject
	}
}

export default new IdOfClientColumnUtils()
